package cn.iocoder.yudao.module.train.service.staff;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.WxUser.WxUserDO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StaffUserDTO;
import cn.iocoder.yudao.module.train.dal.mysql.WxUser.WxUserMapper;
import cn.iocoder.yudao.module.train.utils.DateUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.staff.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.staff.StaffMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 工作人员用户 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private WxUserMapper wxUserMapper;

    @Override
    public Integer createStaff(StaffSaveReqVO createReqVO) {
        // 插入
        StaffDO staff = BeanUtils.toBean(createReqVO, StaffDO.class);
        staff.setStaffAge(DateUtils.calculateAge(staff.getStaffBirthdate()));
        staffMapper.insert(staff);

        // 返回
        return staff.getId();
    }

    @Override
    public void updateStaff(StaffSaveReqVO updateReqVO) {
        // 校验存在
        validateStaffExists(updateReqVO.getId());
        if (wxUserMapper.selectById(updateReqVO.getUserId()) == null) {
            throw exception(WX_USER_NOT_EXISTS);
        }
        // 更新
        StaffDO updateObj = BeanUtils.toBean(updateReqVO, StaffDO.class);
        updateObj.setStaffAge(DateUtils.calculateAge(updateObj.getStaffBirthdate()));
        WxUserDO wxUpdateObj = BeanUtils.toBean(updateReqVO, WxUserDO.class);
        wxUpdateObj.setId(updateObj.getUserId());
        wxUserMapper.updateById(wxUpdateObj);
        staffMapper.updateById(updateObj);
    }

    @Override
    public void deleteStaff(Integer id) {
        // 校验存在
        validateStaffExists(id);
        // 删除
        staffMapper.deleteById(id);
    }

    @Override
        public void deleteStaffListByIds(List<Integer> ids) {
        // 删除
        staffMapper.deleteByIds(ids);
        }


    private void validateStaffExists(Integer id) {
        if (staffMapper.selectById(id) == null) {
            throw exception(STAFF_NOT_EXISTS);
        }
    }

    @Override
    public StaffUserDTO getStaff(Integer id) {
        return staffMapper.selectStaffUser(id);
    }

    @Override
    public PageResult<StaffUserDTO> getStaffPage(StaffPageReqVO pageReqVO) {
        return staffMapper.selectPage(pageReqVO);
    }

}
